<template>
	<view class="all-main">
		<u-loading-page :loading="loadding" loading-mode="semicircle" loading-text="Hello Pro"></u-loading-page>
		<view v-if="!loadding" class="main">
			<view class="lists">
				<view class="item-title">
					<u--text prefixIcon="bag" iconStyle="font-size: 19px" text="终端"></u--text>
				</view>
				<view class="items" v-for="(item, index) in checkData" :key="index">
					<view class="pro-con">
						<view class="left">
							<image :src="item.image"></image>
						</view>
						<view class="right">
							<view class="pro_title">
								{{item.name}}
							</view>
							<view class="pro_money">
								<text class="pro_money_box">{{item.price}}</text>
								<text>x{{item.nums}}</text>
							</view>
							<view class="total_money">
								<text>{{item.nums*item.price}}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="pro-total">
				<view class="p-t-item">
					<text>合计</text>
				</view>
				<u-line></u-line>
				<view class="all-total">
					<text>数量</text>
					<text class="money-red">{{all_nums}}</text>
				</view>
				<u-line dashed></u-line>
				<view class="all-total">
					<text>总计</text>
					<text class="money-red">{{all_money}}</text>
				</view>
			</view>
			<view class="pay-type">
				<view class="title">
					支付方式
				</view>
				<u-line></u-line>
				<radio-group @change="handleChange">
					<view class="pay-type-item">
						<view class="left">
							<u-icon class="pay-type-icon" size="22" color="#19be6b" name="weixin-fill"></u-icon>
							<text>微信</text>
						</view>
						<view class="right">
							<label class="radio">
								<radio value="2" :checked="pay_type == 2" />
							</label>
						</view>
					</view>
					<u-line dashed></u-line>
					<view class="pay-type-item">
						<view class="left">
							<u-icon class="pay-type-icon" size="22" color="#2979ff" name="red-packet"></u-icon>
							<text>余额</text>
							<text class="balace"> ({{balace}})</text>
						</view>
						<view class="right">
							<label class="radio">
								<radio value="1" :checked="pay_type == 1" />
							</label>
						</view>
					</view>
					<u-line dashed></u-line>
					<view class="pay-type-item">
						<view class="left">
							<u-icon class="pay-type-icon" size="22" color="#fa3534" name="bag"></u-icon>
							<text>认购量</text>
							<text class="balace"> ({{subscribe}})</text>
						</view>
						<view class="right">
							<label class="radio">
								<radio value="3" :checked="pay_type == 3" />
							</label>
						</view>
					</view>
				</radio-group>
			</view>
			<view class="buy-operate">
				<view class="total">
					<text></text>
					<text class="total-price">{{total}}</text>
				</view>
				<view class="buy-box">
					<text class="buy-btn" @click="settle">结算</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	/*#ifdef H5*/
	var wx = require('@/util/jweixin-module.js');
	/*#endif*/
	export default {
		data() {
			return {
				loadding: true,
				show: false,
				balace: 0,
				subscribe: 0,
				wxInfo: {},
				userinfo: {},
				checkData: [],
				subData: [],
				all_money: 0,
				all_nums: 0,
				total: 0,
				pay_type: 2,
			}
		},
		onLoad(e) {
			this.userinfo = uni.getStorageSync('userinfo');
			this.checkData = uni.getStorageSync('selectData');
			this.getBalace()
			this.calculate()
			this.superSubcNumFun()
		},
		onShow() {
			this.superSubcNumFun()
		},
		onBackPress(e) {
			uni.navigateBack({
				delta: 1 // 返回的页面数，如果为1表示返回上一页
			});
		},
		methods: {
			// 结算
			settle() {
				var that = this
				// if (this.pay_type == 2) {
				// 	uni.showToast({
				// 		title: '微信支付正在开发中...',
				// 		icon: "none"
				// 	});
				// 	return false
				// }
				if (this.pay_type == 1) {
					if (this.all_money * 100 > this.balace * 100) {
						uni.showToast({
							title: '余额不足',
							icon: "none"
						});
						return false
					}
				}
				if (this.pay_type == 3) {
					if (this.all_nums * 100 > this.subscribe * 100) {
						uni.showToast({
							title: '认购量不足',
							icon: "none"
						});
						return false
					}
				}
				uni.showModal({
					title: '提示',
					content: '确定要结算吗？',
					success: function(res) {
						if (res.confirm) {
							that.createOrder()
						}
					}
				});
			},
			// 创建订单
			async createOrder() {
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp5({
					url: 'Order/saveOrder',
					data: {
						product: this.checkData,
						all_money: this.all_money,
						all_nums: this.all_nums
					},
					header: {
						'token': this.userinfo.token
					}
				});
				uni.hideLoading()
				if (res.code == 1) {
					uni.setStorageSync('selectData', [])
					this.orderPay(res.data.id)
				} else {
					uni.showToast({
						title: res.msg,
						icon: "none"
					})
				}
			},
			// 订单支付
			async orderPay(orderid) {
				var that = this
				const openid = uni.getStorageSync('openid')
				uni.showLoading({
					title: '支付中'
				});
				const {
					data: res
				} = await this.$myHttp5({
					url: 'Order/pay',
					data: {
						order_id: orderid,
						pay_type: this.pay_type,
						openid: openid
					},
					header: {
						'token': this.userinfo.token
					}
				});
				uni.hideLoading()
				if (res.code == 1) {
					if (this.pay_type == 2) {
						this.wxpay(res.data)
					} else {
						uni.navigateTo({
							url: '/pages/version5/index/shop/paysuccess?money=' + that.total + '&pay_type=' + that.pay_type
						})
					}
				} else {
					uni.showToast({
						title: res.msg
					})
					setTimeout(function() {
						uni.navigateBack({
							delta: 1 // 返回的页面数，如果为1表示返回上一页
						});
					}, 1000)
				}
			},
			// 微信支付
			async wxpay(payInfo) {
				var that = this
				wx.config({
					debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
					appId: payInfo.appId, // 必填，公众号的唯一标识
					timestamp: payInfo.timeStamp, // 必填，生成签名的时间戳
					nonceStr: payInfo.nonceStr, // 必填，生成签名的随机串
					signature: payInfo.paySign, // 必填，签名
					jsApiList: ['chooseWXPay'] // 必填，需要使用的JS接口列表
				})
				wx.ready(function() {
					wx.chooseWXPay({
						timestamp: payInfo.timeStamp, // 时间戳
						nonceStr: payInfo.nonceStr, // 随机数
						package: payInfo.package, //
						signType: payInfo.signType,
						paySign: payInfo.paySign, // 签名
						success: function() {
							uni.showToast({
								title: '支付成功'
							})
							setTimeout(function() {
								uni.navigateTo({
									url: '/pages/version5/index/shop/paysuccess?money=' +
										that.total + '&pay_type=' + that.pay_type
								})
							}, 1000)
						},
						cancel: function() {
							uni.showToast({
								title: '取消支付',
								icon: 'none'
							})
							setTimeout(function() {
								uni.navigateBack({
									delta: 1 // 返回的页面数，如果为1表示返回上一页
								});
							}, 1000)
						},
						fail: function() {
							uni.showToast({
								title: '支付失败',
								icon: 'none'
							})
							setTimeout(function() {
								uni.navigateBack({
									delta: 1 // 返回的页面数，如果为1表示返回上一页
								});
							}, 1000)
						}
					})
				})
			},
			// 获取余额
			async getBalace() {
				const {
					data: res
				} = await this.$myHttp5({
					url: 'balance/getTotal',
					header: {
						'token': this.userinfo.token
					}
				});
				this.loadding = false
				if (res.code == 1) {
					this.balace = res.data
				}
			},
			// 获取认购数
			async superSubcNumFun() {
				const {
					data: res
				} = await this.$myHttp5({
					url: 'Buying/person_subc_nums',
					header: {
						'token': this.userinfo.token
					}
				});
				if (res.code == 1) {
					this.subscribe = res.data.nums
				}
			},
			// 支付方式切换
			handleChange(e) {
				this.pay_type = e.detail.value
				if (this.pay_type == 3) {
					this.total = this.all_nums
				} else {
					this.total = this.all_money
				}
			},
			// 统计
			calculate() {
				for (var i in this.checkData) {
					this.all_money += this.checkData[i].nums*this.checkData[i].price
					this.all_nums += this.checkData[i].nums
				}
				this.total = this.all_money
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #f3f4f6;
	}

	.all-main {

		.main {
			padding: 20rpx 20rpx 100rpx 20rpx;

			.address {
				padding: 20rpx;
				background-color: #ffffff;
				border-bottom-left-radius: 20rpx;
				border-bottom-right-radius: 20rpx;
				margin-bottom: 20rpx;

				.address-default {
					.top {
						font-size: 28rpx;
						font-weight: bold;
						display: flex;
						justify-content: space-between;
					}

					.bottom {
						padding-top: 10rpx;
						font-size: 24rpx;

						.phone {
							margin-left: 10rpx;
						}
					}
				}

				.address-null {
					text-align: center;
					font-size: 34rpx;
					padding: 14rpx;
				}
			}

			.lists {
				margin-bottom: 20rpx;
				
				.item-title {
					background-color: #ffffff;
					display: block;
					font-size: 26rpx;
					padding: 20rpx;
					margin-bottom: 20rpx;
				}

				.items {
					padding: 0 30rpx;
					background-color: #ffffff;
					border-radius: 20rpx;
					margin-bottom: 10px;

					.pro-con {
						padding: 20rpx 0;
						display: flex;
						justify-content: space-between;

						.left {
							width: 30%;

							image {
								border-radius: 10rpx;
								width: 160rpx;
								height: 160rpx;
							}
						}

						.right {
							width: 68%;

							.pro_title {
								font-size: 28rpx;
								font-weight: bold;
							}

							.pro_money {
								padding-right: 20rpx;
								font-size: 26rpx;
								line-height: 80rpx;
								display: flex;
								justify-content: space-between;

								.pro_money_box {
									font-weight: bold;
									color: #fa3534;
								}
							}

							.total_money {
								color: #fa3534;
								font-weight: bold;
								text-align: right;
								padding-right: 20rpx;
							}
						}
					}
				}
			}

			.pro-total {
				padding: 10rpx 30rpx;
				margin-bottom: 20rpx;
				background-color: #ffffff;
				border-radius: 20rpx;

				.p-t-item {
					padding: 20rpx 0 30rpx 0;
					font-size: 29rpx;
					font-weight: bold;
					display: flex;
					justify-content: space-between;

					.delivery {
						.delivery-icon {
							float: right;
							margin-left: 10rpx;
							margin-top: 4rpx;
						}
					}

					.freight {
						text {
							float: left;
						}

						.freight-icon {
							float: left;
							margin-left: 20rpx;
							margin-top: 6rpx;
						}
					}
				}

				.all-total {
					margin-top: 10rpx;
					padding: 20rpx 0;
					padding-right: 20rpx;
					font-size: 28rpx;
					text-align: right;
					display: flex;
					justify-content: space-between;

					.money-red {
						color: #fa3534;
						font-weight: bold;
					}
				}
			}

			.pay-type {
				background-color: #ffffff;
				border-radius: 20rpx;
				padding: 20rpx;
				
				.title {
					padding: 10rpx 0 30rpx 0;
					font-size: 29rpx;
					font-weight: bold;
				}

				.pay-type-item {
					padding: 30rpx 0;
					font-size: 30rpx;
					display: flex;
					justify-content: space-between;

					.left {
						.pay-type-icon {
							float: left;
							margin-right: 10rpx;
						}

						.balace {
							margin-left: 14rpx;
							font-size: 24rpx;
							color: #c8c9cc;
						}
					}
				}
			}

			.buy-operate {
				position: fixed;
				left: 0;
				right: 0;
				bottom: 0;
				border-top: 1px solid #f4f4f5;
				background-color: #ffffff;
				display: flex;
				justify-content: space-between;
				padding: 10rpx;
				line-height: 60rpx;

				.total {
					padding-left: 10rpx;
					font-size: 26rpx;
					font-weight: bold;
					color: #fa3534;

					.total-price {
						font-size: 34rpx;
					}
				}

				.buy-box {

					.buy-btn {
						font-size: 26rpx;
						padding: 10rpx 40rpx;
						background-color: #fa3534;
						border-radius: 10rpx;
						color: #ffffff;
					}
				}
			}
		}
	}
</style>